
const NewsModel = require("../../models/NewsModel")

const NewsService = {
  add: async ({ title, content, category, isPublish, editTime, coverPicture }) => {
    return NewsModel.create({ title, content, category, isPublish, editTime, coverPicture })
  },
  getList: async (id) => {
    return id ? NewsModel.find({ _id: id },) : NewsModel.find({},)
  },
  delOne: async ({ _id }) => {
    return NewsModel.deleteOne({ _id })
  },
  updateOne: async ({title, content, category, isPublish, editTime, coverPicture,_id}) => {
     // 有无更换图片
     if (coverPicture) {
      return NewsModel.updateOne({ _id }, {
        title, content, category, isPublish, editTime,  coverPicture
      })
    } else {
      return NewsModel.updateOne({ _id }, {
        title, content, category, isPublish, editTime,
      })
    }
  },
  publish: async ({ _id, isPublish, editTime }) => {
    return NewsModel.updateOne({ _id }, { isPublish, editTime })
  },
}
module.exports = NewsService
